package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.service.system.model.dto.RoleDto;
import com.zhuhjay.service.system.model.vo.RoleInfoVo;
import com.zhuhjay.service.system.service.SystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-02-25
 */
@Api(tags = "角色管理")
@Validated
@RestController
@RequestMapping("/role")
public class SystemRoleController extends BaseController {

    @Resource
    private SystemRoleService roleService;

    @ApiOperation("获取角色列表")
    @PreAuthorize("@perm.hasPerm('system:authority:role:list')")
    @GetMapping("/list")
    public List<RoleInfoVo> list(@RequestParam(required = false, defaultValue = "false") Boolean needMenus) {
        // 角色列表中的 menuIds 字段将只包含叶子节点的 id
        return roleService.getList(needMenus);
    }

    @ApiOperation("添加角色信息")
    @PreAuthorize("@perm.hasPerm('system:authority:role:add')")
    @PostMapping("/add")
    public void add(@Valid @RequestBody RoleDto dto) {
        // 角色列表中的 menuIds 字段将只包含叶子节点的 id
        roleService.add(dto);
    }

    @ApiOperation("修改角色信息")
    @PreAuthorize("@perm.hasPerm('system:authority:role:update')")
    @PutMapping("/update/{id}")
    public void update(@PathVariable Integer id, @Valid @RequestBody RoleDto dto) {
        // 不需要对超级管理员角色进行修改
        checkUpdateAdminRole(id);
        // 角色列表中的 menuIds 字段将只包含叶子节点的 id
        roleService.update(id, dto);
    }

    @ApiOperation("删除角色信息")
    @PreAuthorize("@perm.hasPerm('system:authority:role:delete')")
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        // 禁止删除超级管理员角色
        checkUpdateAdminRole(id);
        roleService.delete(id);
    }

}
